<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>表格Demo</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css" media="all">
</head>

<body>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>添加</legend>
    </fieldset>

    <form class="layui-form" action="" lay-filter="example" enctype="multipart/form-data" method="post" style="padding: 20px;">
        <div class="layui-form-item">
            <label class="layui-form-label">影人名称</label>
            <div class="layui-input-block">
                <input type="text" name="performerName" id="performerName" lay-verify="required" placeholder="请输入"
                    autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">影人头像:</label>
            <div class="layui-input-block" style="margin-right: 20px">
                <div class="layui-upload-list" id="performerImg"
                    style="height: 100px; width: 100px;border: 1px solid #cccccc;"></div>
            </div>
        </div>
        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">影人类型</label>
            <div class="layui-input-inline">
                <select name="performerType" id='performerType'></select>
            </div>
        </div>
        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">影人介绍</label>
            <div class="layui-input-block">
                <input type="text" name="performerContent" id="performerContent" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <script src="../../assets/libs/layui/layui.js" charset="utf-8"></script>
    <script>

        layui.config({
            base: '/module/'
        }).use(['jquery', 'layer', 'table', 'form', 'config', 'laydate', 'upload'], function () {

            var $ = layui.jquery;
            var layer = layui.layer;
            var form = layui.form;
            var config = layui.config;
            var laydate = layui.laydate;
            var upload = layui.upload;
            var files = null;

            $.ajax({
                url: config.base_server + 'GetperformerType',
                dataType: 'json',
                type: 'get',
                async: false,
                success: function (data) {
                    data = data.data;
                    $.each(data, function (index, item) {
                        $('#performerType').append(new Option(item.performerName));
                    });
                    form.render("select");
                }
            })

            upload.render({
                elem: '#performerImg'
                , auto: false
                , choose: (obj) => {
                    obj.preview((index, file, result) => {
                        let imgUrl = result;
                        files = file;
                        $('#performerImg').html('<div class="imgBox" ><img src="' + result + '" alt="' + file.name + '" class="layui-upload-img" width="100" height="100"><a class="remove_' + index + ' imgitem  layui-btn layui-btn-xs layui-btn-danger demo-delete">取消</a></div>')
                        $('.remove_' + index).bind('click', function (e) {
                            e.stopPropagation()
                            files = null;
                            $(this).parent().remove();
                        })
                    });
                }
            });

            form.on('submit(submit)', function (info) {
                var data = form.val('example');
                var { performerName, performerType, performerContent } = data;
                var formData = new FormData();
                formData.append('performerName', performerName);
                formData.append('performerType', performerType);
                formData.append('performerContent', performerContent);
                if (files) {
                    formData.append('performerImg', files)
                } else {
                    layer.msg('用户头像不能为空', { icon: 5 });
                    return false;
                }
                $.ajax({
                    type: "POST",
                    url: config.base_server + "addPerformer",
                    async: false,
                    data: formData,
                    async: false,
                    processData: false,
                    contentType: false,
                    dataType: "json",
                    success: function (data) {
                        if (data.code == 1) {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            console.log("可以关闭嘛？");
                            alert("添加成功");
                            parent.layer.close(index); //再执行关闭   
                        }
                    }
                });
            });



        });
    </script>


</body>